
 #include <QtGui>
 #include <QList>
 #include <QObject>
 #include <QtOpenGL>
 #include <iostream.h>
 #include <math.h>
#include <QPainter>
 #include "glwidget.h"
 #include "window.h"
 #include "include/dto/3d/matrix.h"

#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE  0x809D
#endif

 GLWidget::GLWidget(QWidget *parent)
     : QGLWidget(parent)
 {

	 textPos=0;
 	count=0;
     object = 0;
     xRot = 0;
     yRot = 0;
     zRot = 0;
	pos=0;
	list=0;
	img.load("/video/pics/blumenrahmen.png");
	peter.load("/video/pics/peter.png");
	barbara.load("/video/pics/barbara.png");
	martina.load("/video/pics/martina.png");
	setAutoFillBackground(false);
	text = "";
	text1="";
	text2="";
	text3="";
	text4="";
	text5="";
	QTimer *timer = new QTimer( this );
	     connect( timer, SIGNAL(timeout()), this, SLOT(timerDone()) );
	     timer->start( 30 );
 }

 GLWidget::~GLWidget()
 {
     makeCurrent();
     glDeleteLists(object, 1);
 }

 QSize GLWidget::minimumSizeHint() const
 {
	 if (IsControl::control())
//		 return QSize(1160, 778);
		 return QSize(1000, 750);
	 else
		 return QSize(1024, 768);
 }

 QSize GLWidget::sizeHint() const
 {
	 if (IsControl::control())
//		 return QSize(1200, 650);
		 return QSize(1000, 750);
	 else
	 	return QSize(1024, 768);
 }







void GLWidget::setRenderList(std::list<Renderable *> *l) {
	list = l;
}

 void GLWidget::initializeGL()
 {
//	 bool ok;
//	 QFont font = QFontDialog::getFont(
//	                 &ok, QFont("Helvetica [Cronyx]", 10), this);


 }

void GLWidget::timerDone() {
	update();
}
void GLWidget::showEvent(QShowEvent *event) {
	Q_UNUSED(event);
}
 void GLWidget::paintEvent(QPaintEvent *event)
 {

	 makeCurrent();
	 glMatrixMode(GL_MODELVIEW);
	 glPushMatrix();

//	 qglClearColor(QColor(255, 255, 255));

	 glShadeModel(GL_SMOOTH);
	      glEnable(GL_DEPTH_TEST);
	      glEnable(GL_CULL_FACE);
//	      glEnable(GL_LIGHTING);
//	      glEnable(GL_LIGHT0);
	      glEnable(GL_MULTISAMPLE);
	      glEnable(GL_TEXTURE_2D);

	      setupViewport();

	 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	 //	 glClear( GL_DEPTH_BUFFER_BIT);
	 glLoadIdentity();
	 //     setupViewport(width(),height());

	 if (list==0)
		 printf("empty");
	 else {
		 renderer.render(this,list);
	 }
	 glMatrixMode(GL_MODELVIEW);
	 glPopMatrix();


	 QPainter painter;
	 painter.begin(this);
	 drawInstructions(&painter);

	 painter.end();

 }

 void GLWidget::setupViewport() {

		 if (IsControl::control()) {
			 glViewport(0,0,1200,700);
			 glMatrixMode(GL_PROJECTION);
			 glLoadIdentity();
			 glFrustum(-0.5,0.5,0.5,-0.5,5.0,6.0);

		 }
		 else {
			 glViewport(0,0,1024,768);
			 glMatrixMode(GL_PROJECTION);
			 glLoadIdentity();
			 glFrustum(-0.6,0.6,0.5,-0.5,5.0,6.0);
		 }

		 glMatrixMode(GL_MODELVIEW);
 }

 void GLWidget::setText(QString _text) {
	 text5=text4;
	 text4=text3;
	 text3=text2;
	 text2=text1;
	 text1=text;
	 text = _text;
//	 textPos = 1024;
 }

 void GLWidget::drawInstructions(QPainter *painter)
  {
	 int alpha = 0;
	 if (text=="Zeigs durch die Blume") {

		 painter->drawImage(20,20,img);
	 }
	 else if (text=="Kamerakind Peter") {

			 painter->drawImage(20,60,peter);
		 }
	 else if (text=="Kamerakind Barbara") {

	 			 painter->drawImage(20,60,barbara);
	 		 }
	 else if (text=="Kamerakind Martina") {

		 			 painter->drawImage(20,60,martina);
		 		 }
	 else {
		 alpha = 127;
	 }
	 int heigh = 600;

      QFont font("TlwgMono",12);
      setFont(font);
      QFontMetrics metrics = QFontMetrics(font);
      int border = qMax(4, metrics.leading());

//      QRect rect = metrics.boundingRect(0, heigh, width() - 2*border, int(height()*0.125),
//                                        Qt::AlignLeft | Qt::TextWordWrap, text);
//      painter->setRenderHint(QPainter::TextAntialiasing);
//      painter->setPen(Qt::white);
//      painter->fillRect(QRect(0, heigh, width(), rect.height() + 2*border),
//                        QColor(0, 0, 0, 127));
//      painter->drawText(textPos, border+heigh,
//                        rect.width(), rect.height(),
//                        Qt::AlignLeft | Qt::TextWordWrap, text);

      QRect rect = metrics.boundingRect(0, heigh, width() - 2*border, int(height()*0.125),
    		  Qt::AlignLeft , text);
//      painter->setRenderHint(QPainter::TextAntialiasing);
      painter->setPen(Qt::green);
//      painter->fillRect(QRect(0, heigh, width(), rect.height() + 2*border),
//    		  QColor(0, 0, 0, alpha));
      painter->drawText(textPos, border+heigh+100,
    		  1024, rect.height(),
    		  Qt::AlignLeft, text);
      painter->drawText(textPos, border+heigh+80,
    		  1024, rect.height(),
          		  Qt::AlignLeft, text1);
      painter->drawText(textPos, border+heigh+60,
    		  1024, rect.height(),
          		  Qt::AlignLeft, text2);
      painter->drawText(textPos, border+heigh+40,
    		  1024, rect.height(),
          		  Qt::AlignLeft, text3);
      painter->drawText(textPos, border+heigh+20,
    		  1024, rect.height(),
          		  Qt::AlignLeft, text4);
      painter->drawText(textPos, border+heigh,
    		  1024, rect.height(),
          		  Qt::AlignLeft, text5);

//      textPos--;
//      textPos--;
//      textPos--;
//
//      if (textPos<(0-rect.width())) {
//    	  textPos = 1024;
//      }


//
//      QImage img2;
//      img2.load("/video/pics/plankton-klein-2.png");
//      painter->drawImage(800,50,img2);

  }

 void GLWidget::resizeGL(int width, int height)
 {
	 printf("resizeGL\n");
     int side = qMin(width, height);
     glViewport((width - side) / 2, (height - side) / 2, side, side);

     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
     glMatrixMode(GL_MODELVIEW);
 }

 void GLWidget::mousePressEvent(QMouseEvent *event)
 {
     lastPos = event->pos();
 }

 void GLWidget::mouseMoveEvent(QMouseEvent *event)
 {
     int dx = event->x() - lastPos.x();
     int dy = event->y() - lastPos.y();
     lastPos = event->pos();
 }



//	void GLWidget::pipe() {
//		glBegin(GL_QUADS);
//			GLdouble cx=0;
//    		GLdouble cy=0;
//    		GLdouble cz=0;
//    		float height=0.8;
//    		int corners = {1,5,20,45,12,1};
//    		int heightCount =6;
//    		int height,corner;
//    		for (height=0;height<heightCount-1;height++) {
//    			GLdouble startPoint=height/heightCount-height/2;
//    			int bottomCount=corners[height];
//    			int topCount =corners[height];
//    		}
//		glEnd();
//	}





 void GLWidget::normalizeAngle(int *angle)
 {
     while (*angle < 0)
         *angle += 360 * 16;
     while (*angle > 360 * 16)
         *angle -= 360 * 16;
 }
